package com.app.simpsonux;


import java.io.File;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import android.content.SharedPreferences;

public class SettingViewActivity extends Activity implements View.OnClickListener 
{
    String mUserId = "";
    Button btnBack;
    Button btnSave;
    
    @Override
    protected void onCreate( Bundle savedInstanceState )
    {
        UXParcel sParcel = getIntent().getExtras().getParcelable( getString( R.string.intent_userinfo ) );
        if( sParcel != null )
        {
            mUserId = sParcel.mUserId;
        }
        else
        {
            UXUt.AlertDlg(this, toString(), "Parcel error...", false);
        }
        super.onCreate( savedInstanceState );
        setContentView( R.layout.settingview );
        ( (Button)findViewById( R.id.buttonBack ) ).setOnClickListener(this);
        ( (Button)findViewById( R.id.buttonSave ) ).setOnClickListener(this);
        ( (Button)findViewById( R.id.buttonRemove ) ).setOnClickListener(this);
            
        loadConf();
        
    }
	
	@Override
	public void onClick(View v)
	{
		switch( v.getId() )
		{
		case R.id.buttonRemove:
			removeConf();
			setResult(DialerViewActivity.RESULT_REMOVE);
			finish();
			break;
    	case R.id.buttonSave:
			setResult(DialerViewActivity.RESULT_SUCCESS);
    	    saveConf();
    	case R.id.buttonBack:
    		setResult(DialerViewActivity.RESULT_FAIL);
    		finish();
    		break;
        default:
        	break;
		}
	}
	
	public void removeConf()
	{
	    SharedPreferences.Editor sEdit = 
	    		getSharedPreferences(
                        mUserId,
                        MODE_PRIVATE ).edit();
	    
	    sEdit.clear();
	    File f=new File( getString( R.string.pref_file_path ) + mUserId + ".xml" );
	    if( f.delete() != true )
	        System.out.println("file didn't delet");
	    sEdit.commit();
	    
	    
	}
	
	public boolean saveConf()
	{
        String sUserName       = "";
        String sUserId         = "";
        String sUserPassword   = "";
        String sSVProviderAddr = "";
        String sSVProviderPort = "";
	    
	    SharedPreferences sPreference = getSharedPreferences(
                                           mUserId,
                                           MODE_PRIVATE );
	    
        SharedPreferences.Editor sEdit = sPreference.edit( );
	    
        // User Name
	    sUserName = ( ( EditText )findViewById( R.id.editTextName ) ).getText().toString();
	    sEdit.putString(
	            getString( R.string.pref_field_user_name ),
	            sUserName );
	    
	    // User ID
	    sUserId = ( ( EditText )findViewById( R.id.editTextUserID ) ).getText().toString();
	    sEdit.putString(
	            getString( R.string.pref_field_user_id ),
	            sUserId );
	    
	    // User Password
	    sUserPassword = ( ( EditText )findViewById( R.id.editTextPassword) ).getText().toString();
	    sEdit.putString(
	            getString( R.string.pref_field_user_password ),
	            sUserPassword );
	    
	    // Service Provider Address
	    sSVProviderAddr = ( ( EditText )findViewById( R.id.editTextAddress ) ).getText().toString();
	    sEdit.putString(
	            getString( R.string.pref_field_server_addr ),
	            sSVProviderAddr );
	    
	    // Service Provider Port
	    sSVProviderPort = ( ( EditText )findViewById( R.id.editTextSVPort ) ).getText().toString();
	    sEdit.putString(
	            getString( R.string.pref_field_server_port ),
	            sSVProviderPort );
	    
	    sEdit.commit();
	    
	    return true;
	}
	
	public boolean loadConf()
	{
        String sUserName       = "";
        String sUserId         = "";
        String sUserPassword   = "";
        String sSVProviderAddr = "";
        String sSVProviderPort = "";

	    SharedPreferences sPreference = getSharedPreferences(
	            mUserId,
	            MODE_PRIVATE );
        
	    // User Name
	    sUserName = sPreference.getString(
               getString( R.string.pref_field_user_name ),
               "" );
        ( ( EditText )findViewById( R.id.editTextName ) ).setText( sUserName );

        // User ID
        sUserId = sPreference.getString(
                getString( R.string.pref_field_user_id ),
                "" );
        ( ( EditText) findViewById( R.id.editTextUserID ) ).setText( sUserId );
        
        // User Password
        sUserPassword = sPreference.getString( 
                getString( R.string.pref_field_user_password ),
                "" );
        ( ( EditText ) findViewById( R.id.editTextPassword ) ).setText( sUserPassword );
        
        // Service Provider Address
        sSVProviderAddr = sPreference.getString(
                getString( R.string.pref_field_server_addr ),
                "" );
        ( ( EditText ) findViewById( R.id.editTextAddress) ).setText( sSVProviderAddr );
        
        // Service Provider Port
        sSVProviderPort = sPreference.getString(
                getString( R.string.pref_field_server_port ),
                "" );
        ( ( EditText ) findViewById( R.id.editTextSVPort ) ).setText( sSVProviderPort );
	    
        return true;
	}

};
